package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {


    @Autowired
    private  ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
@Autowired
private  RoomInfoMapper roomInfoMapper;
    @Autowired
    private LabelInfoMapper  labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Override
    //保存或更新
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
           boolean isUpdate=apartmentSubmitVo.getId()!=null;

            //公寓的基本信息直接插入
           //因为apartmentSubmitVo继承了ApartmentInfo所以直接用进行了
        super.saveOrUpdate(apartmentSubmitVo);
           //传过的公寓信息已经存在的 我们则执行更新操作 这样无论是新增公寓或更新公寓都能实现
            //这里是将公寓的老信息都删除掉 然后再插入新的信息
           if (isUpdate) {
               //1.删除图片列表
               LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
               //将属于公寓类型和该公寓的图片删除
                graphQueryWrapper.eq(GraphInfo::getItemType ,ItemType.APARTMENT);
                graphQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
                graphInfoService.remove(graphQueryWrapper);
               //2.删除配套列表
               LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
               apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
                 apartmentFacilityService.remove(apartmentFacilityQueryWrapper);
               //3.删除标签列表
               LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
                    apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
                    apartmentLabelService.remove(apartmentLabelQueryWrapper);
               //4.删除杂费列表
               LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
                apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
                apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);

           }

        //1.插入图片列表
        List<GraphVo>  graphVoList =apartmentSubmitVo.getGraphVoList();
           //前端传来的信息不为空才插入
           if (!CollectionUtils.isEmpty(graphVoList)) {
               //因为我们接收的图片是图片的vo类型的 所以要插入图片要转换为图片类型
               ArrayList<GraphInfo> graphInfoList = new ArrayList<>() ;
                //遍历每个vo并将其的名字和url赋值给图片实体类
               //并将图片的类型和图片的所属公寓赋值
               for (GraphVo graphVo :graphVoList){
                   GraphInfo graphInfo = new GraphInfo();
                   graphInfo.setItemType(ItemType.APARTMENT);
                   graphInfo.setName(graphVo.getName());
                   graphInfo.setUrl(graphVo.getUrl());
                   graphInfo.setItemId(apartmentSubmitVo.getId());
                   graphInfoList.add(graphInfo);
               }
                   graphInfoService.saveBatch(graphInfoList);
           }
             //2.插入配套列表 四个插入的思路都与1相同
        List<Long> facilityIdList= apartmentSubmitVo.getFacilityInfoIds();

        if (!CollectionUtils.isEmpty(facilityIdList)){
            ArrayList<ApartmentFacility> apartmentFacilityList = new ArrayList<>();

            for (Long id:facilityIdList) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setFacilityId(id);
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacilityList.add(apartmentFacility);
            }
              apartmentFacilityService.saveBatch(apartmentFacilityList);

        }

        //3.插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }


        //4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }


    }

    @Override//根据省份 城市 地区 分页查询
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {

            IPage<ApartmentItemVo> result= apartmentInfoMapper.pageItem(page,queryVo);
        return  result;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //查询公寓信息
              ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //查询图片列表
                   List<GraphVo > graphVoList=  graphInfoMapper.selctListByItemTypeAndId(ItemType.APARTMENT,id);

        //查询标签列表
                         List<LabelInfo> labelInfoList =labelInfoMapper.selectListByApartmentId(id);


        //查询配套列表
        List <FacilityInfo> facilityInfos =facilityInfoMapper.selectListByApartmentId(id);

        //查询杂费列表
        List<FeeValueVo> feeValueVoList =feeValueMapper.selectListByApartmentId(id);

        //组装结果
        ApartmentDetailVo apartmentDetailVo =new ApartmentDetailVo();
            //新工具类 我们获得的属性一个一个set太麻烦  该工具类方法是将源类中的属性 直接拷贝到目标类的属性当中 只要属性名字相同就行
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        //先判断该公寓下有没有房间 如果有房间不允许删除公寓
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        Long l = roomInfoMapper.selectCount(roomInfoLambdaQueryWrapper);
        if (l>0){

            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }


        //1.删除图片列表
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        //将属于公寓类型和该公寓的图片删除
        graphQueryWrapper.eq(GraphInfo::getItemType ,ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId,id);

        graphInfoService.remove(graphQueryWrapper);
        //2.删除配套列表
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityQueryWrapper);
        //3.删除标签列表
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelQueryWrapper);
        //4.删除杂费列表
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
        //删除公寓本身
        super.removeById(id);
    }

    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<ApartmentInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApartmentInfo::getId,id);
        updateWrapper.set(ApartmentInfo::getIsRelease,status);
        super.update(updateWrapper);
    }

    @Override
    public List<ApartmentInfo> listInfoByDistrictId(Long id) {
        LambdaQueryWrapper<ApartmentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> list = super.list(queryWrapper);
        return  list;
    }
}




